// Thud - a dynamic object-oriented language
// Copyright (C) 2009,2010 David H. Hovemeyer <david.hovemeyer@gmail.com>

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

/*
 * ConstantPool.h
 *
 *  Created on: Nov 24, 2009
 *      Author: dhovemey
 */

#ifndef CONSTANTPOOL_H_
#define CONSTANTPOOL_H_

#include <cstdint>
#include <cstdio>
#include <vector>
#include <map>
#include "vm/Constant.h"
#include "vm/IPersistent.h"

/**
 * Total ordering for Constants (accessed through pointers).
 */
struct ConstantComparator {
	bool operator()(const Constant *left, const Constant* right) const {
		return Constant::lessThan(left, right);
	}
};

/**
 * Pool of Constant values referenced by bytecode instructions.
 * Each Class has its own ConstantPool.
 */
class ConstantPool : public IPersistent {
private:
	typedef std::vector<Constant*> ConstantList;
	typedef std::map<Constant*, uint16_t, ConstantComparator> ConstantMap;

	ConstantList m_pool;
	ConstantMap m_constantMap; // used to ensure no duplicates are added
	bool m_frozen;

	ConstantPool(const ConstantPool &);
	ConstantPool& operator=(const ConstantPool &);

public:
	/** Default constructor. Creates an empty constant pool. */
	ConstantPool();

	/** Destructor. */
	virtual ~ConstantPool();

	/**
	 * Adopt given Constant, adding it to the pool.
	 * Duplicate constants are discarded (and the index of the existing
	 * equal constant returned).
	 *
	 * @param c a Constant to adopt
	 * @return the reference (index) of the Constant
	 */
	uint16_t addAndAdopt(Constant *c);

	/**
	 * Get the Constant at given index.
	 */
	Constant *getConstant(uint16_t cpIndex);

	/**
	 * Get the number of Constants in the pool.
	 */
	uint32_t getNumConstants() const { return m_pool.size(); }

	/**
	 * Create a ConstantString.
	 *
	 * @param str string value of the constant string to create
	 * @return index of the ConstantString
	 */
	uint16_t createConstantString(const std::wstring &str);

	/**
	 * Create a ConstantSymbol.
	 *
	 * @param symName name of the constant symbol to create
	 * @return index of the ConstantSymbol
	 */
	uint16_t createConstantSymbol(const std::wstring &symName);

	/**
	 * Create a ConstantClass.
	 *
	 * @param clsName name of the constant class to create
	 * @return index of the ConstantClass
	 */
	uint16_t createConstantClass(const std::wstring &clsName);

	/**
	 * Create a ConstantInt32.
	 *
	 * @param value value of the constant integer to create
	 * @return index of the ConstantClass
	 */
	uint16_t createConstantInt32(int32_t value);

	/**
	 * Create a ConstantBoolean.
	 *
	 * @param value value of the constant boolean to create
	 * @return index of the ConstantBoolean
	 */
	uint16_t createConstantBoolean(bool value);

	/**
	 * "Freeze" this ConstantPool.  No more Constants may be added
	 * after freezing.  Some memory may be freed up.
	 */
	void freeze();

	virtual void read(DataInputStream *in);
	virtual void write(DataOutputStream *out) const;
};

#endif /* CONSTANTPOOL_H_ */
